package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.admin.mall.bo.OrderRefundBo;
import com.cskaoyan.bean.admin.mall.bo.OrderShipBo;
import com.cskaoyan.bean.admin.mall.vo.OrderDetailVo;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.admin.OrderService;
import com.cskaoyan.util.OrderStatusEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Name : OrderServiceImpl.java
 * @Time : 2021/8/12 20:21
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public BaseRespData list(String userId, String orderSn, Short[] orderStatusArray, BaseParamBo baseParamBo) {
        PageHelper.startPage(baseParamBo.getPage(), baseParamBo.getLimit());
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(baseParamBo.getSort() + " " + baseParamBo.getOrder());
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null && !"".equals(userId)) {
            if(!userId .matches("\\d+")){
                return null;
            }
            int id = Integer.parseInt(userId);
            criteria.andUserIdEqualTo(id);
        }
        if (orderSn != null && !"".equals(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (orderStatusArray != null && orderStatusArray.length != 0) {
            List<Short> shorts = Arrays.asList(orderStatusArray);
            criteria.andOrderStatusIn(shorts);
        }
        List<Order> orders = orderMapper.selectByExample(orderExample);
//        ArrayList<OrderVo> orderVos = new ArrayList<>();
//        for (Order order : orders) {
//            OrderVo orderVo = new OrderVo(order.getConsignee(), order.getAddress(), order.getComments(), order.getAddTime(), order.getOrderSn(), order.getActualPrice(), order.getMobile(), order.getOrderStatus(), order.getUpdateTime(), order.getMessage(), order.getUserId(), order.getGrouponPrice(), order.getDeleted(), order.getGoodsPrice(), order.getCouponPrice(), order.getOrderPrice(), order.getId(), order.getEndTime(), order.getFreightPrice(), order.getIntegralPrice());
//            orderVos.add(orderVo);
//        }
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        long total = pageInfo.getTotal();
        return BaseRespData.create(orders, total);
    }

    @Override
    public OrderDetailVo detail(Integer id) {
        OrderGoodsExample example = new OrderGoodsExample();
        example.createCriteria().andDeletedEqualTo(false).andOrderIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(example);
        Order order = orderMapper.selectByPrimaryKey(id);
        User user = userMapper.selectByPrimaryKey(order.getUserId());
        OrderDetailVo.UserEntity userEntity = new OrderDetailVo().new UserEntity(user.getNickname(), user.getAvatar());
        OrderDetailVo orderDetailVo = new OrderDetailVo(orderGoods, userEntity, order);
        return orderDetailVo;
    }

    @Override
    public void ship(OrderShipBo orderShipBo) {
        Order order = new Order();
        order.setOrderStatus(new Short("" + OrderStatusEnum.DELIVERED.getStateId()));
        order.setId(orderShipBo.getOrderId());
        order.setShipChannel(orderShipBo.getShipChannel());
        order.setShipSn(orderShipBo.getShipSn());
        Date date = new Date();
        order.setShipTime(date);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void refund(OrderRefundBo orderRefundBo) {
        Order order = new Order();
        order.setId(orderRefundBo.getOrderId());
        order.setOrderStatus(new Short("" + OrderStatusEnum.REFUNDED.getStateId()));
        Date date = new Date();
        order.setEndTime(date);
        order.setUpdateTime(date);
        orderMapper.updateByPrimaryKeySelective(order);
    }
}
